package com.thinksns.sociax.t4.android.user;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.thinksns.sociax.android.R;
import com.thinksns.sociax.component.CustomTitle;
import com.thinksns.sociax.component.LeftAndRightTitle;
import com.thinksns.sociax.t4.android.Thinksns;
import com.thinksns.sociax.t4.android.ThinksnsAbscractActivity;
import com.thinksns.sociax.t4.android.data.StaticInApp;
import com.thinksns.sociax.t4.android.video.ToastUtils;
import com.thinksns.sociax.t4.component.SmallDialog;
import com.thinksns.sociax.t4.model.ModelUser;
import com.thinksns.tschat.adapter.AdapterChatDetailList;
import com.thinksns.tschat.db.SQLHelperChatMessage;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 类说明：
 *
 * @author Administrator
 * @version 1.0
 * @date 2014-11-10
 */
public class ActivityEditInfo extends ThinksnsAbscractActivity {
    private int type = 0; // 传入参数，是修改昵称/备注/
    TextView tv_name;
    EditText ed_info;
    Button bt_save;
    ModelUser user = Thinksns.getMy();
    public static String CENTER_TITLE = "修改昵称";
    private String uid = ""; // 修改备注传入用户 uid
    private String mRemark = ""; // 修改备注传入用户备注
    private SmallDialog smallDialog;
    //4.5版本增加修改备注
    private Handler uiHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            smallDialog.dismiss();
            String result = (String) msg.obj;
            try {
                JSONObject js = new JSONObject(result);
                String message = js.getString("msg");
                if (js.getInt("status") == 1) {
                    AdapterChatDetailList.uid=uid;
                    AdapterChatDetailList.remark = ed_info.getText().toString().trim();
                    try {
                        SQLHelperChatMessage.upDateChatListRemark(Integer.valueOf(uid), ed_info.getText().toString().trim());
                        SQLHelperChatMessage.upDateRoomListRemark(Integer.valueOf(uid), ed_info.getText().toString().trim());
                        SQLHelperChatMessage.upDateCreateRoomListRemark(Integer.valueOf(uid), ed_info.getText().toString().trim());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    Intent intent = new Intent();
                    intent.putExtra("input", ed_info.getText().toString().trim());
                    setResult(RESULT_OK, intent);
                    finish();
                    ToastUtils.showToast(message);
                } else {
                    ToastUtils.showToast(message);
                }
            } catch (Exception e) {
                ToastUtils.showToast("修改备注失败");
                e.printStackTrace();
            }

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initIntentData();
        initView();
        initListener();
        initData();
    }

    private void initData() {

    }

    private void initListener() {
        bt_save.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if (!TextUtils.isEmpty(uid)) {
                    //4.5版本增加修改备注
                    saveUserInfoThread(uid, ed_info.getText().toString().trim());
                } else {

                    Intent intent = new Intent();
                    intent.putExtra("input", ed_info.getText().toString().trim());
                    setResult(RESULT_OK, intent);
                    finish();
                }


            }
        });
    }

    private void initView() {
        smallDialog = new SmallDialog(this, getString(R.string.please_wait));
        tv_name = (TextView) findViewById(R.id.tv_name);
        ed_info = (EditText) findViewById(R.id.edit_info);
        bt_save = (Button) findViewById(R.id.bt_save);

        //设置光标在文本的末尾
        if (type == StaticInApp.CHANGE_USER_NAME) {
            tv_name.setText("昵称");
            ed_info.setText(user.getUserName());

            ed_info.setSelection(user.getUserName().length());
        } else if (type == StaticInApp.CHANGE_USER_INTRO) {
            tv_name.setText("简介");
            ed_info.setText(user.getIntro());
            ed_info.setMinLines(5);

            ed_info.setSelection(user.getIntro().length());
        } else if (type == StaticInApp.CHANGE_USER_BEIZHU) {
            tv_name.setText("备注");
            if (TextUtils.isEmpty(mRemark)){
                mRemark = getString(R.string.hint_no_remark);
            }
            ed_info.setHint(mRemark);
            //4.5版本增加修改备注
        }
    }

    private void initIntentData() {
        type = getIntent().getIntExtra("type", 0);
        //4.5版本增加修改备注
        uid = getIntent().getStringExtra("uid");
        mRemark = getIntent().getStringExtra("remark");
        if (type == 0)
            finish();
    }

    @Override
    public String getTitleCenter() {
        return CENTER_TITLE;
    }

    @Override
    protected CustomTitle setCustomTitle() {
        return new LeftAndRightTitle(R.drawable.img_back, this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_edit_info_common_old;
    }

    @Override
    protected void onPause() {
        super.onPause();
        InputMethodManager imm = (InputMethodManager) getSystemService(
                Context.INPUT_METHOD_SERVICE);
        if (ed_info != null)
            imm.hideSoftInputFromWindow(ed_info.getWindowToken(), 0);
    }

    // ***************修改信息*****************//
    private void saveUserInfoThread(final String uid, final String input) {
        smallDialog.show();
        new Thread(new Runnable() {

            @Override
            public void run() {
                Thinksns app = (Thinksns) getApplication();
                Message msg = uiHandler.obtainMessage();
                Object result = false;
                try {
                    result = app.getUsers().setRemark(input, uid);
                } catch (Exception e) {
                    e.printStackTrace();
                    smallDialog.dismiss();
                }
                msg.obj = result;
                uiHandler.sendMessage(msg);
            }
        }).start();
    }
}
